<?php

/**
 * This is the model class for table "employee".
 *
 * The followings are the available columns in table 'employee':
 * @property integer $id
 * @property string $emp_firstname
 * @property string $emp_lastname
 * @property string $emp_middlename
 * @property string $emp_contact
 * @property string $emp_address
 * @property integer $pharmacy_id
 * @property string $emp_type
 * @property string $emp_username
 * @property string $emp_password
 *
 * The followings are the available model relations:
 * @property Pharmacy $pharmacy
 * @property Employeelog[] $employeelogs
 * @property Orderheader[] $orderheaders
 * @property Prescriptionheader[] $prescriptionheaders
 */
class Employee extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'employee';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('emp_firstname, emp_lastname, emp_middlename, emp_contact,emp_address, pharmacy_id, emp_type, emp_username, emp_password', 'required'),
			array('pharmacy_id', 'numerical', 'integerOnly'=>true),
			array('emp_firstname, emp_lastname, emp_middlename, emp_contact,emp_address, emp_type, emp_username', 'length', 'max'=>45),
			array('emp_password', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, emp_firstname, emp_lastname, emp_middlename, emp_contact, emp_address, pharmacy_id, emp_type, emp_username, emp_password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pharmacy' => array(self::BELONGS_TO, 'Pharmacy', 'pharmacy_id'),
			'employeelogs' => array(self::HAS_MANY, 'Employeelog', 'employee_id'),
			'orderheaders' => array(self::HAS_MANY, 'Orderheader', 'employee_id'),
			'prescriptionheaders' => array(self::HAS_MANY, 'Prescriptionheader', 'employee_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'emp_firstname' => 'Emp Firstname',
			'emp_lastname' => 'Emp Lastname',
			'emp_middlename' => 'Emp Middlename',
			'emp_contact' => 'Emp Contact',
			'emp_address' => 'Emp Address',
			'pharmacy_id' => 'Pharmacy',
			'emp_type' => 'Emp Type',
			'emp_username' => 'Emp Username',
			'emp_password' => 'Emp Password',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('emp_firstname',$this->emp_firstname,true);
		$criteria->compare('emp_lastname',$this->emp_lastname,true);
		$criteria->compare('emp_middlename',$this->emp_middlename,true);
		$criteria->compare('emp_contact',$this->emp_contact,true);
		$criteria->compare('emp_address',$this->emp_address,true);
		$criteria->compare('pharmacy_id',$this->pharmacy_id);
		$criteria->compare('emp_type',$this->emp_type,true);
		$criteria->compare('emp_username',$this->emp_username,true);
		$criteria->compare('emp_password',$this->emp_password,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Employee the static model class
	 */
	public static function model($className=__CLASS__)
	{
		 return parent::model($className);
	 
	}
	
	public function beforeSave(){
                $this->emp_password = hash_hmac('sha256', $this->emp_password, 
                Yii::app()->params['encryptionKey']);           
                return parent::beforeSave();
        }
}